/*
 * @Title HelloXML.java
 * @Description
 * @Copyright Copyright(c) 2018
 * @Company com.lizhen
 * @author lizhen
 * @version 0.0.1
 */
package com.lizhen.mybatis.core;

import com.lizhen.mybatis.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

/**
 * @author lizhen
 */
public class HelloXML {
    private static final String CONFIG_PATH = "config/Configure.xml";
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static {
        try {
            reader = Resources.getResourceAsReader(CONFIG_PATH);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession() {
        return sqlSessionFactory;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SqlSession session = getSession().openSession();
        User user = (User) session.selectOne("com.lizhen.mybatis.entity.UserMapper.GetUserByID", 1);
        if (user != null) {
            String userInfo = "用户是" + user.getName() + ", 部门是" + user.getDept() + ", 网站是·" + user.getWebsite();

            System.out.println(userInfo);
        }
        session.close();
    }

}
